package com.crrc.yzt.demo.vm

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.crrc.yzt.demo.model.CarDetail
import com.crrc.yzt.demo.model.ItemCar
import com.crrc.yzt.demo.model.ItemCarGroup
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import java.time.Duration
import kotlin.random.Random
import kotlin.random.nextInt

class CarVM : ViewModel() {

    private val _carDetailStateFlow: MutableStateFlow<CarDetail?> = MutableStateFlow(null)
    val carDetailStateFlow = _carDetailStateFlow.asStateFlow()


    fun loadCarDetail(){

        viewModelScope.launch {

            repeat(Int.MAX_VALUE){
                val list = mutableListOf<ItemCarGroup>()

                list.add(loadGroup("车辆基本信息",1))
                list.add(loadGroup("车辆状态信息",2))
                list.add(loadGroup("车辆健康检测",3))

                val carDetail = CarDetail(list)

                _carDetailStateFlow.emit(carDetail)

                delay(1000)

            }

        }

    }


    fun loadGroup(nameGroup:String,index:Int):ItemCarGroup{

        val itemCarGroup = ItemCarGroup(nameGroup)

        itemCarGroup.childList = loadChild(index)

        return itemCarGroup
    }

    var speedyList = listOf("49.31Km/h","45.33Km/h","49.35Km/h","49.43Km/h","49.31Km/h","50.66Km/h")
    var openList = listOf("50.24%","49.21%","48.62%","52.32%","51.88%","48.266%")
    var closeList = listOf("0.24%","0.21%","0.62%","0.32%","0.88%","0.266%")


    fun loadChild(group:Int):List<ItemCar>{

        val list = mutableListOf<ItemCar>()

        val index = Random.nextInt(IntRange(0,5))

        Log.i("sss", "loadChild: $index")

        when(group){
            1->{
                list.add(ItemCar("车牌号：湘A57148D"))
                list.add(ItemCar("公告号：TEG6158BEV01"))
                list.add(ItemCar("车架号：LSFGA2301MD404029"))
            }
            2->{
                list.add(ItemCar("档位状态：D1档"))
                list.add(ItemCar("VCU版本信息：1.2.1.10"))
                list.add(ItemCar("车速：${speedyList[index]}"))
                list.add(ItemCar("油门开度：${openList[index]}"))
                list.add(ItemCar("刹车开度：${closeList[index]}"))
                list.add(ItemCar("VCU-Life值：99"))
            }
            3->{
                list.add(ItemCar("VCU系统： 正常"))
                list.add(ItemCar("转向系统： 正常"))
                list.add(ItemCar("冷却系统： 一级故障"))
                list.add(ItemCar("动力系统： 正常"))
                list.add(ItemCar("制动系统： 正常"))
            }
        }

        return list
    }





}